//
//  changeEmailViewController.m
//  忘不了
//
//  Created by rimi on 16/7/16.
//  Copyright © 2016年 handuan. All rights reserved.
//

#import "changeEmailViewController.h"
#import "SDAutoLayout.h"
#import "BaseRequest+MoneyAndRemember.h"
#import "CheckWeb.h"
@interface changeEmailViewController ()<UITextFieldDelegate>

@end

@implementation changeEmailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"修改邮箱";
    
    UIImageView *backGroudView = [[UIImageView alloc]initWithFrame:self.view.frame];
    backGroudView.image = [UIImage imageNamed:@"s.jpg"];
    [self.view addSubview:backGroudView];
    
    [self loadUI];
}

- (void)loadUI {
    NSArray *array = @[@"账号",@"密码",@"新的邮箱"];
    for (int i=0; i<3; i++) {
        UILabel *label = [[UILabel alloc]init];
        label.text = array[i];
        label.textColor = [UIColor brownColor];
        label.textAlignment = NSTextAlignmentLeft;
        label.font = [UIFont systemFontOfSize:18];
        [self.view addSubview:label];
        label.sd_layout
        .leftSpaceToView(self.view,30)
        .rightSpaceToView(self.view,30)
        .topSpaceToView(self.view,150+self.view.frame.size.width*0.2*i+10*i)
        .heightIs(self.view.frame.size.width*0.1);
    }
    for (int j=0; j<3; j++) {
        UITextField *textField = [[UITextField alloc]init];
        textField.textColor = [UIColor blackColor];
        textField.font = [UIFont systemFontOfSize:20];
        textField.layer.cornerRadius = 10;
        textField.layer.masksToBounds = YES;
        textField.tag = 400 + j;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.backgroundColor = [UIColor whiteColor];
        if (textField.tag == 401) {
            textField.secureTextEntry = YES;
        }
        if (textField.tag != 402) {
            textField.delegate = self;
        }
        [self.view addSubview:textField];
        textField.sd_layout
        .leftSpaceToView(self.view,30)
        .rightSpaceToView(self.view,30)
        .heightIs(self.view.frame.size.width*0.1)
        .topSpaceToView(self.view,150+self.view.frame.size.width*0.1*(j+1)+10*j+self.view.frame.size.width*0.1*j);
    }
    UIButton *changeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [changeButton setTitle:@"修改" forState:UIControlStateNormal];
    changeButton.backgroundColor = [UIColor colorWithRed:0.8784 green:0.8431 blue:0.7765 alpha:1.0];
    [changeButton setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
    changeButton.layer.cornerRadius = 10;
    changeButton.layer.borderColor = [UIColor colorWithRed:0.5727 green:0.5812 blue:0.4312 alpha:1.0].CGColor;
    changeButton.layer.borderWidth = 1;
    [changeButton addTarget:self action:@selector(changeButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:changeButton];
    changeButton.sd_layout
    .centerXIs(self.view.frame.size.width/2)
    .bottomSpaceToView(self.view,self.view.frame.size.width*0.4)
    .widthIs(self.view.frame.size.width*0.3)
    .heightIs(self.view.frame.size.width*0.1);
    
}

- (void)changeButtonPressed {
    UITextField *userName = [self.view viewWithTag:400];
    UITextField *password = [self.view viewWithTag:401];
    UITextField *newEmail = [self.view viewWithTag:402];
    if ([userName.text isEqual:@""] || [password.text isEqual:@""] || [newEmail.text isEqual:@""]) {
        UIAlertController *tip1 = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请填写完整" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [tip1 addAction:sure];
        [self presentViewController:tip1 animated:YES completion:nil];
    } else if ([self isValidateEmail:newEmail.text] == NO) {
        UIAlertController *tip2 = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"邮箱输入错误" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [tip2 addAction:sure];
        [self presentViewController:tip2 animated:YES completion:nil];
    } else {
        BOOL isWeb = [CheckWeb checkWang];
        if (isWeb == NO) {
            UIAlertController *tip3 = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请检查网络" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:tip3 animated:YES completion:nil];
            [self performSelector:@selector(disMissed) withObject:nil afterDelay:0.1];
        }else{
            [BaseRequest updateEmailWithUserName:userName.text password:password.text NewEmail:newEmail.text completionhanlder:^(Response *response) {
                if (response.isSuccess) {
                    UIAlertController *tip3 = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"修改邮箱成功" preferredStyle:UIAlertControllerStyleAlert];
                    [self presentViewController:tip3 animated:YES completion:nil];
                    [self performSelector:@selector(disMiss) withObject:nil afterDelay:0.1];
                    userName.text = @"";
                    password.text = @"";
                    newEmail.text = @"";
                } else {
                    UIAlertController *tip4 = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"修改邮箱失败" preferredStyle:UIAlertControllerStyleAlert];
                    [self presentViewController:tip4 animated:YES completion:nil];
                    [self performSelector:@selector(disMissed) withObject:nil afterDelay:0.1];
                }
            }];
        }
    }
}

//正则表达式验证邮箱
- (BOOL)isValidateEmail:(NSString *)email{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}

//文本框限定最大长度
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (range.location >= 15) {
        return NO;
    }
    return YES;
}

//dismiss 警告提示框
- (void)disMiss
{
    [self dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)disMissed
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

//点空白回收键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


@end
